package cn.com.b2office.bean;
/**
 * @author  zhenggang
 * @version Jul 23, 2008 11:21:29 AM
 * comment  
 */
public class Pagination {
	
	private int totalRecordNum;	// 总记录数 
	private int totalPageNum; //一共多少页
	private int curPageNum ;  //当前页码数 (默认为1)
	private int maxResults;	//每页显示记录数
	private int firstResult;  //记录开始位置
	private int endResult;		//记录结束位置
	
	public void init(int curPageNum, int totalRecordNum,int maxResults){
		this.setTotalRecordNum(totalRecordNum);		
		this.setMaxResults(maxResults);
		this.setTotalPageNum();  
		this.setCurPageNum(curPageNum);
		this.setFirstResult();
		this.setEndResult();
	}
	
	public void init( int totalRecordNum,int maxResults){
		this.setTotalRecordNum(totalRecordNum);		
		this.setMaxResults(maxResults);
		this.setTotalPageNum();  
		this.setCurPageNum(curPageNum);
		this.setFirstResult();
	}
	
	public int getTotalRecordNum() {
		return totalRecordNum;
	}

	public void setTotalRecordNum(int totalRecordNum) {
		this.totalRecordNum = totalRecordNum;
	}

	public int getTotalPageNum() {
		return totalPageNum;
	}
	/**	根据总记录数与每页显示数，计算一共多少页	*/
	public void setTotalPageNum() {  
		if(totalRecordNum < maxResults){
			this.totalPageNum = 1;
		}else if(totalRecordNum % maxResults == 0){
			this.totalPageNum = totalRecordNum /maxResults ;
		}else{
			this.totalPageNum = totalRecordNum /maxResults + 1;
		}
	}

	public int getCurPageNum() {
		return curPageNum;
	}
	/** 如果当前页数小于1，则设置当前页数为1    */
	public void setCurPageNum(int curPageNum) {
		if(curPageNum < 1){
			this.curPageNum = 1;
		}else if(curPageNum > totalPageNum){
			this.curPageNum = totalPageNum;
		}else{
			this.curPageNum = curPageNum;
		}
	}

	public int getMaxResults() {
		return maxResults;
	}

	public void setMaxResults(int maxResults) {
		this.maxResults = maxResults;
	}

	public int getFirstResult() {
		return firstResult;
	}

	public void setFirstResult() {
		//this.firstResult = firstResult;
		this.firstResult = (curPageNum-1)*maxResults;
	}

	public int getEndResult() {
		return endResult;
	}

	public void setEndResult() {
		//this.endResult = endResult;
		if(maxResults*curPageNum < totalRecordNum){
			this.endResult = maxResults*curPageNum;
		}else{
			this.endResult = totalRecordNum;
		}
		
	}
}
